package vtt.android.tinthethao_ota.fragments;

import vtt.android.tinthethao_ota.R;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.android.gms.ads.AdView;

import fr.castorflex.android.smoothprogressbar.SmoothProgressBar;

/**
 * A placeholder fragment containing a simple view.
 */
public class LivescoreFragment extends BaseWebFragment {
	
	private View mLPSRootView;
	private String fontSize;
    private MenuItem mMenuLive24h;
    private MenuItem mMenuLivescore;
    private boolean mIsLivescoreActived;
	
	/**
	 * Returns a new instance of this fragment for the given section number.
	 */
	public static LivescoreFragment newInstance(int sectionNumber) {
		LivescoreFragment fragment = new LivescoreFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		fontSize = (mParentAct.getResources().getDisplayMetrics().density  * 110) + "%";
		log.d("VTT>>> fontSize " + fontSize);
		
		mLPSRootView = inflater.inflate(R.layout.fragment_cm_webview, container, false);
		mProgressBar = (SmoothProgressBar) mLPSRootView.findViewById(R.id.progressbar_loading);

		// load admob
		AdView adView = (AdView) mLPSRootView.findViewById(R.id.adView);
		loadAdmob(adView);
				
		// init webview
		mWebView = (WebView) mLPSRootView.findViewById(R.id.webView1);
		// init common webview
		initCommonWebview();
		
		//mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		mWebView.setWebViewClient(new WebClient());
		// init custom config
		mWebView.setLongClickable(false);
		
        if (Build.VERSION.SDK_INT >= 11){
            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
		
		// If there is a previous instance restore it in the webview
	    mWebView.loadUrl(getString(R.string.url_livescore_1));	

		return mLPSRootView;
	}
	
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.menu_livescore, menu);
        
        mMenuLive24h = menu.findItem(R.id.action_menu_livescore_1);
        mMenuLivescore = menu.findItem(R.id.action_menu_livescore_2);
        if(mIsLivescoreActived){
            mMenuLive24h.setVisible(true);
            mMenuLivescore.setVisible(false);
        }
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        log.d("VTT>>> action_refresh" + "");
        switch (item.getItemId()) {
        // page bongda24h
        case R.id.action_menu_livescore_1:
            mWebView.loadUrl(getString(R.string.url_livescore_1));
            mMenuLive24h.setVisible(false);
            mMenuLivescore.setVisible(true);
            mIsLivescoreActived = false;
            return true;
        // page livescore.com
        case R.id.action_menu_livescore_2:
            mWebView.loadUrl(getString(R.string.url_livescore_2));
            mMenuLive24h.setVisible(true);
            mMenuLivescore.setVisible(false);
            mIsLivescoreActived = true;
            return true;
        }
        
        return super.onOptionsItemSelected(item);
    }
	
	class WebClient extends WebViewClient {

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			mProgressBar.progressiveStart();
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
		    super.onPageFinished(view, url);
//		    try {
		        // remove target sdk in manifest = 19 will fix this bug
		        //if(Build.VERSION.SDK_INT < 19){
//		            view.loadUrl(
//                        "javascript: "
//                                + "document.getElementById('header').innerHTML = ''; "
//                                + "document.getElementById('footer').innerHTML = ''; "
//                                + "document.getElementById('TOP_l').innerHTML = ''; "
//                                + "document.getElementById('TOP_p').innerHTML = ''; "
//                                + "document.getElementById('FLOATING_l').innerHTML = ''; "
//                                + "document.getElementById('FLOATING_p').innerHTML = ''; "
//                                
//                                + "document.body.style.fontSize = '" + fontSize + "'; ");
		        //}
//            } catch (Exception e) {
//                // TODO: handle exception
//            }
		            
            if(url.contains("bongda24h.vn")){
                // remove all google ads
                view.loadUrl(
                        "javascript: var ads = document.getElementsByClassName('adsbygoogle'); for (var i = 0; i < ads.length; ++i) { var item = ads[i]; item.style.display = 'none';} "
                                + " document.getElementsByClassName('header')[0].style.display = 'none'; "
                                + " document.getElementsByClassName('topmenu_bg')[0].style.display = 'none'; "
                );
            }
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
		    // prevent user install another app
            if(url.contains("play.google.com/store/apps") || url.contains("market://details?id=") || url.endsWith(".apk")){
                return true;
            }
			return false;
		}
	}
}